#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
# 
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998
# Netscape Communications Corporation. All Rights Reserved.
# 
# Contributor(s): TomWij <TomWij@live.com>

##############################################################################
#
# getComponents.cgi
# -----------------
# Gets a list of components from the MySQL table 'components' in JSON format.
#
##############################################################################

use DBI;
use DBD::mysql;

print "Content-type: application/json\n\n";

do '..\helpers\query.cgi';

# Get a list of components from the MySQL table 'components'.
do '..\..\..\..\localconfig';
$driver = DBI->install_driver("mysql"); 
$connect = $driver->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass, {}) or print "<b>MySQL: Connection failed.</b>";

$query = "SELECT id, name FROM components WHERE product_id = " . $FORM{id} . " ORDER BY id";
$query_handle = $connect->prepare($query) or print "<b>MySQL: Query Preparation failed.</b>";
$query_handle->execute() or print "<b>MySQL: Query Execution failed.</b>";
$query_handle->bind_columns(undef, \$id, \$name);

# Give those components back in JSON format.
$comma = 0;

print "[";
while($query_handle->fetch()) {
   if ($comma == 1)
   {
      print ",";
   }
   print "[$id,\"$name\"]";
   $comma = 1;
}
print "]";